layui.define(["table", "form"], function(e) {
    var admin = layui.admin;
    var $ = layui.$,
        form = layui.form,
        table = layui.table;
    //监听搜索
    form.on('submit(LAY-user-front-search)', function(data) {
        var field = data.field;
        //执行重载
        table.reload('LAY-user-manage', {
            where: field
        });
    });
    //事件
    var active = {
        batchdel: function() {
            var checkStatus = table.checkStatus('LAY-user-manage'),
                checkData = checkStatus.data; //得到选中的数据
            if (checkData.length === 0) {
                return layer.msg('请选择数据');
            }
            
            layer.confirm('确定禁用吗？', function() {
                //执行 Ajax 后重载
                var _ids = layui.getParamValues(checkData, 'u_id');
                $.delete(layui.setter.app_url + 'user/delete', {
                    id: _ids.join()
                }, function(data) {
                    layer.msg(data.msg);
                    table.reload('LAY-user-manage');
                });
            });
            
        },
        add: function() {
            layer.open({
                type: 2,
                title: '添加用户',
                content: layui.setter.app_url + 'user/create',
                maxmin: true,
                area: ['500px', '450px'],
                btn: ['确定', '取消'],
                yes: function(index, layero) {
                    var iframeWindow = window['layui-layer-iframe' + index],
                        submitID = 'LAY-user-front-submit',
                        submit = layero.find('iframe').contents().find('#' + submitID);
                    //监听提交
                    iframeWindow.layui.form.on('submit(' + submitID + ')', function(data) {
                        var field = data.field; //获取提交的字段             
                        $.post(layui.setter.app_url + 'user/save', field, function(data, textStatus, xhr) {
                            layer.msg(data.msg);
                            if (data.status == 1) {
                                table.reload("LAY-user-manage"), layer.close(index)
                            }
                        });
                    });
                    submit.trigger('click');
                }
            });
        }
    };
    $('.layui-btn.layuiadmin-btn-useradmin').on('click', function() {
        var type = $(this).data('type');
        active[type] ? active[type].call(this) : '';
    });
    //表格内处理     
    table.render({
        elem: "#LAY-user-manage",
        url: app_url + "user/listData",
        cols: [
            [{
                type: "checkbox",
                 
            }, {
                field: "u_id",
                title: "ID",
                sort: !0
            }, {
                field: "avatar",
                title: "头像",
              
                height:120,
                templet: "#imgTpl",
                align:'center',

            }, {
                field: "credit_level_txt",
                title: "信用等级",
                minWidth: 50,
                sort:!0,
                templet: "#levelTpl"
            }, {
                field: "truename",
                title: "真实姓名",
                minWidth: 100,
            },  {
                field: "phone",
                title: "手机"
            }, {
                field: "email",
                title: "邮箱"
            }, {
                field: "sex_text",
                title: "性别"
            }, {
                field: "status_text",
                title: "状态",
            }, {
                field: "money",
                title: "账户金额"
            }, {
                field: "freeze",
                title: "冻结金额"
            }, {
                field: "create_time",
                title: "加入时间",
                sort: !0
            }, {
                title: "操作",
                align: "center",
              
                width: 160,
                toolbar: "#table-useradmin-webuser"
            }]
        ],
        page: !0,
        limit: 10,
        height: "full-220",
        text: "对不起，加载出现异常！"
    }), table.on("tool(LAY-user-manage)", function(e) {
        if ("del" === e.event) {
            layer.confirm("真的删除行么", function(t) {
                $.delete(layui.setter.app_url + 'user/delete?id=' + e.data.u_id, function(data) {
                    layer.msg(data.msg), e.del(), layer.close(t);
                });
            })
        } else if ("edit" === e.event) {
            var _data = e.data;
            layer.open({
                type: 2,
                title: "编辑用户",
                content: app_url + "user/edit?id=" + _data.u_id,
                maxmin: !0,
                area: ["500px", "450px"],
                btn: ["确定", "取消"],
                yes: function(e, t) {
                    var l = window["layui-layer-iframe" + e],
                        r = "LAY-user-front-submit",
                        n = t.find("iframe").contents().find("#" + r);
                    l.layui.form.on("submit(" + r + ")", function(t) {
                        $.put(layui.setter.app_url + 'user/update?id=' + _data.u_id, t.field, function(data, textStatus, xhr) {
                            if (data.status == 1) {
                                table.reload("LAY-user-manage"), layer.close(e)
                            }
                            layui.layer.msg(data.msg);
                        }, 'json');
                    }), n.trigger("click")
                },
                success: function(e, t) {}
            })
        }
    }), e("useradmin", {})
});